package com.pat.java.swing.test;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

//ToolbarFrame2.java
//The Swing-ified button example  
//

public class ToolbarFrameWithSwing extends Frame{

	// This time, let's use JButtons!
	JButton cutButton, copyButton, pasteButton;
	JButton javaButton, macButton, motifyButton, winButton;
	
	public ToolbarFrameWithSwing(){
		super("Toolbar Example (Swing)");
		setSize(450, 250);
		
		addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});	
		
		ActionListener printActionListener = new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println(e.getActionCommand());
			}
		};
		
		 // JPanel works similarly to Panel, so we'll use it.
	    JPanel toolbar = new JPanel( );
	    toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));

		cutButton = new JButton("Cut");
		cutButton.addActionListener(printActionListener);
		toolbar.add(cutButton);
		
		copyButton = new JButton("Copy");
		copyButton.addActionListener(printActionListener);
		toolbar.add(copyButton);
		
		pasteButton = new  JButton("Paste");
		pasteButton.addActionListener(printActionListener);
		toolbar.add(pasteButton);
		
		add(toolbar, BorderLayout.NORTH);
		
		//Add the L&F controls.
		JPanel lnfPanel = new JPanel();
		LnFListener lnfListener = new LnFListener(this);
		
		macButton = new JButton("Mac");
		macButton.addActionListener(lnfListener);
		lnfPanel.add(macButton);
		
		javaButton = new JButton("Metal");
		javaButton.addActionListener(lnfListener);
		lnfPanel.add(javaButton);
		
		motifyButton = new JButton("Motify");
		motifyButton.addActionListener(lnfListener);
		lnfPanel.add(motifyButton);
		
		winButton = new JButton("Windows");
		winButton.addActionListener(lnfListener);
		lnfPanel.add(winButton);
		
		add(lnfPanel, BorderLayout.SOUTH);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ToolbarFrameWithSwing tf2 = new ToolbarFrameWithSwing();
		tf2.setVisible(true);
	}
	
	class LnFListener implements ActionListener{
		Frame frame;
		
		public LnFListener(Frame f) {
			frame = f;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			String lnfName = null;
			if(e.getActionCommand().equals("Mac")){
				lnfName = "com.apple.mrj.swing.MacLookAndFeel";
			}else if(e.getActionCommand().equals("Metal")){
				lnfName = "javax.swing.plaf.metal.MetalLookAndFeel";
			}else if(e.getActionCommand().equals("Motif")){
				lnfName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
			}else if(e.getActionCommand().equals("Windows")){
				lnfName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
			}else{
				System.err.print("Unrecognized L&F request action"+ e.getActionCommand());
			}
			try {
				UIManager.setLookAndFeel(lnfName);
				SwingUtilities.updateComponentTreeUI(frame);
			} catch (ClassNotFoundException e1) {
				System.err.println("LookAndFeel class not found:" + lnfName);
			} catch (InstantiationException e1) {
				System.err.println("Could not load LookAndFeel:" + lnfName);
			} catch (IllegalAccessException e1) {
				System.err.println("Cannot use LookAndFeel:" + lnfName);
			} catch (UnsupportedLookAndFeelException e1) {
				System.err.println("Unsupported LookAndFeel:" + lnfName);
			}
		}
	}
}